//
//  MessageModel.swift
//  HealthMonitoring
//
//  Created by 珠海正杰 on 2018/3/27.
//  Copyright © 2018年 YTYanK. All rights reserved.
//

import UIKit

class MessageModel: IMUIMessageModel {
    open var textMessage: String = ""
    var mediaPath: String = ""
   
//    var user: HMImUserInfo?
    
    override func mediaFilePath() -> String {
        return mediaPath
    }
    
    override var resizableBubbleImage: UIImage {
            // return defoult message bubble
        return super.resizableBubbleImage
    }
    /**
      isOutGoing:  判断是否发出去的还是 接收的数据
      messageStatus: 信息状态
     */
    init(msgId: String, messageStatus: IMUIMessageStatus, fromUser: HMImUserInfo, isOutGoing: Bool, date: Date, type: String, text: String, mediaPath:String, layout: IMUIMessageCellLayoutProtocol, duration: CGFloat?) {
        self.textMessage = text
        self.mediaPath = mediaPath
        super.init(msgId: msgId, messageStatus: messageStatus, fromUser: fromUser, isOutGoing: isOutGoing, time: "", type: type, cellLayout: layout, duration: duration)
    }
    
    /// 发送文本
    convenience init(text: String, isOutGoing: Bool) {
        let layout = MessageCellLayout(isOutGoingMessage: isOutGoing,
                                       isNeedShowTime: false,
                                       bubbleContentSize: MessageModel.calculateTextContentSize(text: text),
                                       bubbleContentInsets: UIEdgeInsets.zero,
                                       timeLabelContentSize: CGSize.zero,
                                       type: "text")
        let msgId = "\(NSDate().timeIntervalSince1970 * 1000)"
        self.init(msgId: msgId, messageStatus: .sending, fromUser: HMImUserInfo(), isOutGoing: isOutGoing, date: Date(), type: "text", text: text, mediaPath: "", layout: layout, duration: nil)
    }
    
    
    /// 参数测试
//    convenience  init(msgId: String, fromUser: IMUIUserProtocol, isOutGoing: Bool,text: String, messageStatus: IMUIMessageStatus) {
//
//        let layout = MessageCellLayout(isOutGoingMessage: isOutGoing,
//                                       isNeedShowTime: false,
//                                       bubbleContentSize: MessageModel.calculateTextContentSize(text: text),
//                                       bubbleContentInsets: UIEdgeInsets.zero,
//                                       timeLabelContentSize: CGSize.zero,
//                                       type: "text")
//
//        let u = HMImUserInfo.init()
//        u.im_displayName = "名称"
//
//        self.init(msgId: msgId, messageStatus: messageStatus, fromUser: u, isOutGoing: isOutGoing, date: Date(), type: "text", text: text, mediaPath: "", layout: layout, duration: 4.0)
//    }
    
    
    
    
    ///  发送语音
    convenience init(voicePath: String, duration: CGFloat, isOutGoing: Bool) {
        let layout = MessageCellLayout(isOutGoingMessage: isOutGoing,
                                       isNeedShowTime: false,
                                       bubbleContentSize: CGSize(width: 80, height: 37),
                                       bubbleContentInsets: UIEdgeInsets.zero,
                                       timeLabelContentSize: CGSize.zero,
                                       type: "voice")
        let msgId = "\(NSDate().timeIntervalSince1970 * 1000)"
        self.init(msgId: msgId, messageStatus: .sending, fromUser: HMImUserInfo(), isOutGoing: isOutGoing, date: Date(), type: "voice", text: "", mediaPath: "", layout: layout, duration: duration)
        
    }
    
    /// 发送图片
    convenience init(imagePath: String, isOutGoing: Bool) {
        let msgId = "\(NSDate().timeIntervalSince1970 * 1000)"
        
        var imgSize = CGSize(width: 120, height: 160)
        
        
        
        if let img = UIImage(contentsOfFile: imagePath) {
            imgSize = MessageModel.covnerImageSize(with: CGSize(width: (img.cgImage?.width)!, height:(img.cgImage?.height)!))
        }
        
        let layout = MessageCellLayout(isOutGoingMessage: isOutGoing,
                                       isNeedShowTime: false,
                                       bubbleContentSize: imgSize,
                                       bubbleContentInsets: UIEdgeInsets.zero,
                                       timeLabelContentSize: CGSize.zero,
                                       type: "image")
        self.init(msgId: msgId, messageStatus: .sending, fromUser: HMImUserInfo(), isOutGoing: isOutGoing, date: Date(), type: "image", text: "", mediaPath: imagePath, layout: layout, duration: nil)
    }
    
    /// 发送自定义文件
    convenience init(data: Data, isOutGoing: Bool, type: String) {
        
        // 标记自定义类型
        UserDefaults.standard.set(type, forKey: "customType");
        var imgSize = CGSize(width: 120, height: 160)
        
         imgSize = MessageModel.covnerImageSize(with: CGSize(width: (imgSize.width), height:(imgSize.height)))
    
        
        let layout = MessageCellLayout(isOutGoingMessage: isOutGoing,
                                       isNeedShowTime: false,
                                       bubbleContentSize: imgSize,
                                       bubbleContentInsets: UIEdgeInsets.zero,
                                       timeLabelContentSize: CGSize.zero,
                                       type: "image")
    
         let path = Bundle.main.path(forResource: "pl", ofType: "png")
         let msgId = "\(NSDate().timeIntervalSince1970 * 1000)"
         self.init(msgId: msgId, messageStatus: .sending, fromUser: HMImUserInfo(), isOutGoing: isOutGoing, date: Date(), type: "image", text: "", mediaPath: path!, layout: layout, duration: nil)
        
    }
    
    
    /// 发送视频
    convenience init(videoPath: String, isOutGoing: Bool) {
        let layout = MessageCellLayout(isOutGoingMessage: isOutGoing,
                                       isNeedShowTime: false,
                                       bubbleContentSize: CGSize(width: 120, height: 160),
                                       bubbleContentInsets: UIEdgeInsets.zero,
                                       timeLabelContentSize: CGSize.zero,
                                       type: "video")
        let msgId = "\(NSDate().timeIntervalSince1970 * 1000)"
        self.init(msgId: msgId, messageStatus: .sending, fromUser: HMImUserInfo(), isOutGoing: isOutGoing, date: Date(), type: "video", text: "", mediaPath: videoPath, layout: layout, duration: nil)
    }
    
    override func text() -> String {
        return self.textMessage
    }
    
    static func calculateTextContentSize(text: String) -> CGSize {
        let textSize = text.sizeWithConstrainedWidth(with: IMUIMessageCellLayout.bubbleMaxWidth, font: UIFont.systemFont(ofSize: 18))
        return textSize
    }
    
    static func calculateNameContentSize(text: String) -> CGSize {
        return text.sizeWithConstrainedWidth(with: 200, font: IMUIMessageCellLayout.timeStringFont)
    }
    
    static func covnerImageSize(with size: CGSize) -> CGSize {
        let maxSide = 160.0
        var scale = size.width / size.height
        if size.width > size.height {
            scale = scale > 2 ? 2 : scale
            return CGSize(width: CGFloat(maxSide), height:CGFloat(maxSide) / CGFloat(scale))
        }else {
            scale = scale < 0.5 ? 0.5 : scale
            return CGSize(width: CGFloat(maxSide) * CGFloat(scale), height: CGFloat(maxSide))
        }
    }
}

//MARK - IMUIMessageCellLayoutProtocol
class MessageCellLayout: IMUIMessageCellLayout {
    var type: String
    
    init(isOutGoingMessage: Bool, isNeedShowTime: Bool, bubbleContentSize: CGSize, bubbleContentInsets: UIEdgeInsets, timeLabelContentSize: CGSize, type: String) {
        self.type = type
        super.init(isOutGoingMessage: isOutGoingMessage, isNeedShowTime: isNeedShowTime, bubbleContentSize: bubbleContentSize, bubbleContentInsets: bubbleContentInsets, timeLabelContentSize: timeLabelContentSize)
        
    }
    
    override var bubbleContentInset: UIEdgeInsets {
        if type != "text" { return UIEdgeInsets.zero }
         if isOutGoingMessage {
           return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 15)
         } else {
           return UIEdgeInsets(top: 10, left: 15, bottom: 10, right: 10)
         }
    }
    
    override var bubbleContentView: IMUIMessageContentViewProtocol {
        if type == "text" {
            return IMUITextMessageContentView()
        }
        
        if type == "image" {
            return IMUIImageMessageContentView()
        }
        
        if type == "voice" {
            return IMUIVoiceMessageContentView()
        }
        
        if type == "video" {
            return IMUIVideoMessageContentView()
        }
        
        return IMUIDefaultContentView()
    }
    
    override var bubbleContentType: String {
        return type
    }
}
